package net.zjitc.controller;


import net.zjitc.pojp.CateGory;
import net.zjitc.pojp.CateGorySecond;
import net.zjitc.pojp.Product;
import net.zjitc.service.AdminCateGorySecondService;
import net.zjitc.service.AdminCateGoryService;
import net.zjitc.service.AdminProductService;
import net.zjitc.service.UserService;
import net.zjitc.service.impl.AdminCateGorySecondServiceImpl;
import net.zjitc.service.impl.AdminCateGoryServiceImpl;
import net.zjitc.service.impl.AdminProductServiceImpl;
import net.zjitc.service.impl.UserServiceImpl;
import net.zjitc.utils.CommonUtils;
import net.zjitc.utils.PageBean;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/11/22.
 */
public class AdminProduct {
    AdminProductService productService=new AdminProductServiceImpl();
    AdminCateGorySecondService adminCateGorySecondService=new AdminCateGorySecondServiceImpl();
    //显示列表
    @URLMapping(url="/AdminProductFindAll")
    public String findAll(HttpServletRequest request, HttpServletResponse response){
       //通过参数传页码
        String pageNum=request.getParameter("page");
        //因为前端传来的是string类型
       int page=Integer.valueOf(pageNum);
        int pageSize=12;
        PageBean<Product> pageBean=productService.findPageBean(page,pageSize);
        request.setAttribute("page",pageBean);

        return "/admin/product/list.jsp";
    }
    //显示添加页面
    @URLMapping(url = "/AdminProductAddUI")
    public String addUI(HttpServletRequest request, HttpServletResponse response){
        //准备二级分类当回显数据
        List<CateGorySecond> list=adminCateGorySecondService.findAll();
        request.setAttribute("list",list);

        return "/admin/product/add.jsp";
    }

    //添加页面
    @URLMapping(url = "/AdminProductAdd")
    public String add(HttpServletRequest request, HttpServletResponse response){

        //1.工厂
        DiskFileItemFactory factory=new DiskFileItemFactory();
        //2.解析器
        ServletFileUpload fileUpload= new ServletFileUpload(factory);

        //编码方式
        fileUpload.setHeaderEncoding("utf-8");
        //3.解析
        Product product=new Product();
        CateGorySecond cateGorySecond=new CateGorySecond();
        try {
            List<FileItem> fileItems=fileUpload.parseRequest(request);
            for(FileItem item:fileItems){
                //处理表单普通数据项
                if(item.isFormField()){
                    if("pname".equals(item.getFieldName())){
                        product.setPname(item.getString("utf-8"));
                    }
                    if("shopPrice".equals(item.getFieldName())){
                         product.setShop_price(Double.valueOf(item.getString()));
                    }
                    if("marketPrice".equals(item.getFieldName())){
                        product.setMarket_price(Double.valueOf(item.getString()));
                    }
                    if("isHot".equals(item.getFieldName())){
                        product.setIs_hot(Integer.valueOf(item.getString()));
                    }
                    if("pdesc".equals(item.getFieldName())){
                        product.setPdesc(item.getString("utf-8"));
                    }
                    if("csid".equals(item.getFieldName())){
                        product.setCateGorySecond(cateGorySecond);
                    }
                }else{//处理文件
                    //文件的输入流 文件名可能一样  products/5
                    String fileName=item.getName();
                    fileName= CommonUtils.getUUID()+fileName;

                    InputStream inputStream=item.getInputStream();
                    /*输入流文件保存到本地（图片的实际位置）*/
                    String path=request.getServletContext().getRealPath("/products/5");
                    File dir=new File(path);
                    if(!dir.exists()){
                        dir.mkdirs();
                    }
                    File file=new File(dir,fileName);
                    item.write(file);
                    //保存到数据库中的路径
                    String imgPath="products/5/"+fileName;
                    product.setImage(imgPath);
                }
            }
            product.setPdate(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }

         int count=productService.insert(product);
        //添加完后。要重定向到...
        String url=request.getContextPath()+"/AdminProductFindAll";
        return "Redirect:"+url;

    }

    //显示修改页面
    @URLMapping(url = "/AdminProductEditUI")
    public String editUI(HttpServletRequest request, HttpServletResponse response){

        return "/admin/product/editUI.jsp";
    }

    //更新功能
    @URLMapping(url = "/AdminProductEdit")
    public String edit(HttpServletRequest request, HttpServletResponse response){

        return "Redirect:"+request.getContextPath()+"/AdminProductFindAll";
    }



    //删除功能
    @URLMapping(url = "/AdminProductDelete")
    public String delete(HttpServletRequest request,HttpServletResponse response){

        return "Redirect:"+request.getContextPath()+"/AdminProductFindAll";
    }

}
